<?php

namespace app\common\model\banxi\sys;

use app\common\enums\MenuEndpointEnum;
use app\common\enums\MenuTypeEnum;
use app\common\model\common\BaseModel;

/**
 * Attachment模型
 * @property string url              文件物理路径
 * @property int quote            上传(引用)次数
 * @property int last_upload_time 最后上传时间
 */
class SysMenu extends BaseModel
{
    protected $name = 'banxi_sys_menu';

    protected $autoWriteTimestamp = true;

    protected $append = [
        'MenuTypeCn',
        'MenuEndpointCn'
    ];

    protected $type = [
        'id' => 'string',
        'createTime' => 'timestamp:Y-m-d H:i:s',
        'updateTime' => 'timestamp:Y-m-d H:i:s',
    ];


    //-----------------------------------------------------------------------------------
    //｜                                   获取器                                         ｜
    //-----------------------------------------------------------------------------------

    public function getMenuTypeCnAttr($value, $data)
    {
        $value = $value ?: ($data['menuType'] ?? '');
        return (new MenuTypeEnum())->getDescription($value) ?? '';
    }

    public function getMenuEndpointCnAttr($value, $data)
    {
        $value = $value ?: ($data['menuEndpoint'] ?? '');
        return (new MenuEndpointEnum())->getDescription($value) ?? '';
    }

    //-----------------------------------------------------------------------------------
    //｜                                   处理方法                                       ｜
    //-----------------------------------------------------------------------------------

    public function listByInId(array $menuIds = [])
    {
        $where = [];
        if ($menuIds) {
            $where = [['id', 'in', $menuIds]];
        }
        return self::where($where)->select();
    }

    public function listTree()
    {
        $menuList = $this->order(['orderNum' => 'asc'])->select()->toArray();
        return $this->buildTree($menuList, function ($menu) {
            return [
                'id' => $menu['id'],
                'label' => $menu['menuName'],
                'parentId' => $menu['parentId'],
                'weight' => $menu['orderNum'],
            ];
        });
    }

    public function buildTree(array $items, callable $mapper)
    {
        $tree = [];
        $itemMap = [];

        // 将所有项按 ID 存入映射表
        foreach ($items as $item) {
            $mappedItem = $mapper($item); // 使用映射函数处理每个项
            $itemMap[$mappedItem['id']] = $mappedItem;
            // $itemMap[$mappedItem['id']]['children'] = [];
        }

        // 构建父子关系
        foreach ($itemMap as &$item) {
            if ($item['parentId'] == 0) {
                // 顶级节点
                $tree[] = &$item;
            } else {
                // 子节点挂载到父节点
                if (isset($itemMap[$item['parentId']])) {
                    $itemMap[$item['parentId']]['children'][] = &$item;
                }
            }
        }

        return $tree;
    }
    //-----------------------------------------------------------------------------------
    //｜                                   模型关联                                       ｜
    //-----------------------------------------------------------------------------------


}